// pages/guashika/guashika.js

const urlAdd = require('../../utils/config').urlAdd;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    form: {
      userAccIdcard: ''
    }

  },
  /**
   * 挂失卡
   */
  guashi_tijiao: function(e) {
    var lost_idcard = e.detail.value.userAccIdcard; //身份证
    var lost_password = e.detail.value.userCardPass; //一卡通密码
    var stuCode = wx.getStorageSync('stuCode'); //学工号
    var lostcardList = this.data.crdAccid; //卡号

    if (lost_idcard == "") {
      wx.showModal({
        title: '提示',
        content: '身份证号不能为空',
        showCancel: false,
        confirmColor: '#30a1db'
      });
      return;
    }
    //验证身份证的正确性
    if ((!(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(lost_idcard)))) {
      wx.showModal({
        title: '提示',
        content: '请输入18位的有效身份证',
        showCancel: false,
        confirmColor: '#30a1db'
      });
      return;
    }
    if (lost_password == "") {
      wx.showModal({
        title: "错误",
        content: "一卡通密码不能为空",
        showCancel: false,
        confirmColor: '#30a1db'
      });
      return;
    }
    // 提交
    wx.request({
      url: urlAdd + "/m/tcommcardController/lockCard?stuCode=" + stuCode + "&idCard=" + lost_idcard + "&cardNo=" + lostcardList + "&pwd=" + lost_password, //接口地址
      data: {
        'stuCode': stuCode,
        'idCard': lost_idcard,
        'cardNo': lostcardList,
        'pwd': lost_password,
      },
      method: "POST",
      header: {
        "content-type": "application/json"
      },
      success: function(result) {
        if (result.data.code == 200) {
          wx.showModal({
            title: '提示',
            content: '挂失成功',
            showCancel:false,
            success: function(res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1
                })
              } 
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '挂失失败',
            showCancel:false,
            success: function(res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1
                })
              } 
            }
          })
        }

      },
      fail: function() {
        //				mui.toast("网络请求超时");
        // wx.showModal({
        //   title: "",
        //   content: "网络请求超时",
        //   showCancel: false,
        //   confirmColor: '#30a1db'
        // });
        wx.showToast({
          title: '网络请求超时',
          image: '/images/erron.png'
        })
      }
      // 提交
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: "挂失",
    });
    var that = this;
    


  },

  findCardByStuCode: function(){
    var that = this;
    var stuCode = wx.getStorageSync('stuCode');
    console.log(that);
    wx.showLoading({
      title: '查询卡信息中...',
    })
    wx.request({
      url: urlAdd + '/m/tcommcardController/findCardByStuCode?stuCode=' + stuCode,
      data: {
        'stuCode': wx.getStorageSync('stuCode'),
      },
      dataType: 'json', //服务器返回json格式数据
      type: 'get', //HTTP请求类型
      timeout: 10000, //超时时间设置为10秒；
      success: function(result) {
        console.log("result=====")
        console.log(result);
        if(result.data.code == '205') {
          wx.hideLoading();
          wx.showModal({
            title: '提示',
            content: '暂无卡片',
            showCancel:false,
            success: function(res) {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1
                })
              } 
            }
          })
        }
        if(result.data.code == '200'){
          for(var i = 0, len = result.data.data.length; i < len; i++){
            console.log(result.data.data[i].crdStatus);
            if(result.data.data[i].crdStatus=='正常'){
              wx.hideLoading();
              that.setData({
                stuCode: wx.getStorageSync('stuCode'),
                crdAccid: result.data.data[i].crdId,
              });
              console.log(stuCode)
              console.log(that.data.crdAccid)
              that.guashi_tijiao();
            }
          }
          wx.hideLoading();
            wx.showModal({
              title: '提示',
              content: '暂无可挂失卡片',
              showCancel:false,
              success: function(res) {
                if (res.confirm) {
                  wx.navigateBack({
                    delta: 1
                  })
                } 
              }
            })
          
        } 
        
      },
      fail: function(res) {
        wx.showToast({
          title: '请求数据超时',
          image: '/images/erron.png',
        })
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    //验证是否授权
    if (wx.getStorageSync('openId') != '') {
      if (wx.getStorageSync('stuCode') == '' || wx.getStorageSync('stuCode') == null) {
        wx.showToast({
          title: '你已授过权, 请登录',
        })
        wx.redirectTo({
          url: '../../pages/login/login?openId=' + wx.getStorageSync('openId')
        })
      } else {
        
        that.findCardByStuCode();
      }
    } else {
      wx.showModal({
        title: '提示',
        content: '你还未授权登录，是否授权登录',
        cancelText: '取消',
        cancelColor: 'red',
        confirmText: '确认',
        confirmColor: '#30a1db',
        success: function (res) {
          //点击取消
          if (res.cancel) {
            // wx.showModal({
            //   title: '提示',
            //   content: '你取消了授权，功能将无法使用，请重新授权',
            //   showCancel: false,
            //   // cancelText: '确认',
            //   // cancelColor: 'red',
            //   confirmText: '去授权',
            //   confirmColor: '#30a1db',
            //   success: function (res) {
            //     wx.redirectTo({
            //       url: '../../pages/userlogin/login'
            //     })
            //   },
            // })
            wx.switchTab({
              url: '../../pages/index/index',
            })
          } else {
            //点击确定
            wx.redirectTo({
              url: '../../pages/userlogin/login'
            })
          }

        },
        fail: function () {}
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },


})